//
//  SWScrollBar.m
//  SWScrollBar
//
//  Created by shaowei on 13-7-9.
//  Copyright (c) 2013年 LianZhan. All rights reserved.
//

#import "SWScrollBar.h"

static int sNoSelectedIndex = -1;

@interface SWScrollBar()

@property (nonatomic,retain) NSArray *itemArray;
@property (nonatomic,assign) int selectedIndex;

@end

@implementation SWScrollBar

- (void)dealloc
{
    _scrollBardataSource = nil;
    _scrollBarDelegate = nil;
    
    [_itemArray release];
    
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self initSetting];
    }
    return self;
}

- (void)awakeFromNib{
    [super awakeFromNib];
    [self initSetting];
}

#pragma mark Function - Private

- (void)initSetting{
    //远程ScrollView 设置
    self.bounces = YES;
    self.scrollEnabled = YES;
    self.alwaysBounceHorizontal = YES;
    self.alwaysBounceVertical = NO;
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
    
    self.backgroundColor = [UIColor clearColor];
    
    //本地自定义设置
    self.scrollBarType = SWScrollBarTypeDefault;
    self.isRepeatResponse = NO;
    self.selectedIndex = 0;

    //    [self reloadData];
}

- (void)setScrollBarWithType:(SWScrollBarType)scrollBarType{
    switch (scrollBarType) {
        case SWScrollBarTypeDefault:
        {
            self.bounces = YES;
            self.scrollEnabled = YES;
            self.alwaysBounceHorizontal = YES;
        }
            break;
        case SWScrollBarTypeDivideEqually:
        {
            self.bounces = NO;
            self.scrollEnabled = NO;
            self.alwaysBounceHorizontal = NO;
        }
            break;
            
        default:
            break;
    }
    
    
}

- (void)setItemSubViewMask:(UIView *)itemView{
    
    NSArray *tempArray = itemView.subviews;
    
    for (UIView *subView in tempArray) {
        subView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin |
        UIViewAutoresizingFlexibleRightMargin |
        UIViewAutoresizingFlexibleTopMargin |
        UIViewAutoresizingFlexibleBottomMargin |
        UIViewAutoresizingFlexibleHeight |
        UIViewAutoresizingFlexibleWidth;
    }
    
    
}


- (void)actionTapGesture:(id)sender{
    UITapGestureRecognizer *tapGesture = (UITapGestureRecognizer *)sender;
    UIView *itemView = (UIView *)tapGesture.view;
    
    if (_isRepeatResponse || (itemView.tag != _selectedIndex)) {
        [self itemSelectedAndCallback:itemView.tag];
    }
}

- (void)removeAllSubView{
    NSArray *subViewArray = self.subviews;
    for (UIView *subView in subViewArray) {
        [subView removeFromSuperview];
    }
}

- (void)itemSelectedOnlyUI:(int)index{
    
    //防止越界
    if (index >= [_itemArray count]) {
        return;
    }
    
    //item响应
    for (UIView<SWScrollItemDelegate> *itemView in _itemArray) {
        if (itemView.tag == index) {
            [itemView setSelected:YES];
            _selectedIndex = index;
        }else{
            [itemView setSelected:NO];
        }
    }
    
    
}

- (void)itemSelectedAndCallback:(int)index{
    [self itemSelectedOnlyUI:index];
    
    //回调代理响应事件
    if ([_scrollBarDelegate respondsToSelector:@selector(scrollBar:didSelectItemAtIndex:)]) {
        [_scrollBarDelegate scrollBar:self didSelectItemAtIndex:index];
    }else{
        DLog(@"SW - delegate-无响应函数 scrollBar:didSelectItemAtIndex:");
    }
}


#pragma mark - Function - Public

- (void)reloadData{
    [self setScrollBarWithType:_scrollBarType];
    [self removeAllSubView];
    
    CGFloat allWidth = 0;
    CGRect scrollFrame = self.frame;
    
    int itemNum = 0;
    if ([_scrollBardataSource respondsToSelector:@selector(itemNumberOfScrollBar:)]) {
        itemNum = [self.scrollBardataSource itemNumberOfScrollBar:self];
    }
    
    CGFloat splitWidth = 0.0f;
    if ([_scrollBarDelegate respondsToSelector:@selector(widthForSplitView:)]) {
        splitWidth = [_scrollBarDelegate widthForSplitView:self];
    }
    
    NSMutableArray *mItemArray = [[NSMutableArray alloc] initWithCapacity:itemNum];
    for (int i = 0; i < itemNum; i++) {
        CGFloat itemWidth = 0.0f;
        
        
        switch (_scrollBarType) {
            case SWScrollBarTypeDefault:
            {
                if ([_scrollBarDelegate respondsToSelector:@selector(scrollBar:widthForItemAtIndex:)]) {
                    itemWidth = [_scrollBarDelegate scrollBar:self widthForItemAtIndex:i];
                }
            }
                break;
            case SWScrollBarTypeDivideEqually:
            {
                itemWidth = (self.frame.size.width - (itemNum - 1) * splitWidth) / itemNum;
            }
                break;
                
            default:
                break;
        }
        
        UIView<SWScrollItemDelegate> *itemView = [_scrollBardataSource scrollBar:self itemAtIndex:i];
        if (itemView) {
            [mItemArray addObject:itemView];
        }
        
        //添加时间响应
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                                     action:@selector(actionTapGesture:)];
        [itemView addGestureRecognizer:tapGesture];
        
        //设置子View自动布局
//        [self setItemSubViewMask:itemView];
        
        //添加tap
        itemView.tag = i;
        
        //设置为 “未选中状态”
        if (_selectedIndex == i) {
            [itemView setSelected:YES];
        }else{
            [itemView setSelected:NO];
        }
        
        [tapGesture release];
        
        //添加itemView 计算宽度
        if ((fabsf(itemWidth - 0.0f)  < FLOAT_MINVALUE)) {
            itemWidth = itemView.frame.size.width;
        }
        
        CGRect itemFrame = CGRectMake(allWidth, 0, itemWidth, scrollFrame.size.height);
        
        //        if (fabsf(scrollFrame.size.height - itemFrame.size.height) > FLOAT_MINVALUE) {
        //            //两个View  高度不匹配  item 垂直方向 居中
        //            itemView.center = CGPointMake(itemView.center.x, scrollFrame.size.height*0.5f);
        //        }
        itemView.frame = itemFrame;
        
        [self addSubview:itemView];
        allWidth += itemWidth;
        
        //添加分隔View
        UIView *splitView = nil;
        if ((i <(itemNum - 1)) &&
            [_scrollBardataSource respondsToSelector:@selector(splitViewOfScrollBar:)]) {
            
            splitView = [_scrollBardataSource splitViewOfScrollBar:self];
        }
        
        if (splitView && (splitWidth > FLOAT_MINVALUE) ) {
            //分隔栏的宽度高度 自动伸缩
            CGRect splitFrame = CGRectMake(allWidth, 0, splitWidth, scrollFrame.size.height);
            splitView.frame = splitFrame;
            [self addSubview:splitView];
            
            allWidth += splitWidth;
        }
    }
    
    self.itemArray = mItemArray;
    [mItemArray release];
    
    
    if (allWidth < self.frame.size.width) {
        allWidth = self.frame.size.width;
    }
    
    if (self.scrollBarType != SWScrollBarTypeDivideEqually) {
        self.contentSize = CGSizeMake(allWidth, scrollFrame.size.height);
    }else{
        self.contentSize = scrollFrame.size;
    }
    
    //    [self layoutSubviews];
}

- (void)setSelectedItemIndexOnlyUI:(int)index{
    [self itemSelectedOnlyUI:index];
}

- (void)setSelectedItemIndexAndCallback:(int)index{
    [self itemSelectedAndCallback:index];
}

- (void)cancelAllSelected{
    _selectedIndex = sNoSelectedIndex;
    for (UIView<SWScrollItemDelegate> *itemView in _itemArray) {
        if ([itemView getSelectedStatus]) {
            [itemView setSelected:NO];
        }
    }
}

- (int)getCurrentSelectedIndex{
    return _selectedIndex;
}

@end
